package com.nmgolden.lanxin.model;

import lombok.Data;

import java.time.Duration;
import java.time.Instant;

/**
 * 访问令牌
 * 可代表app token，js api token
 *
 * @author siger
 */
@Data
public class AccessToken {
    private String token;
    private int expiresIn;
    /**
     * 对象创建时间（对象初始化时间，必然是 apptoken 在服务器实际产生时间之后）
     */
    private Instant fetchTimePoint;

    public AccessToken() {
    }

    public AccessToken(String token, int expiresIn) {
        this.token = token;
        this.expiresIn = expiresIn;
        this.fetchTimePoint = Instant.now();
    }

    public boolean isExpire() {
        Duration timeElapsed = Duration.between(this.fetchTimePoint, Instant.now());
        return timeElapsed.getSeconds() >= this.expiresIn;
    }
}